package com.atguigu.testdemo;

import com.atguigu.spring5.aopanno.User;
import com.atguigu.spring5.config.SpringConfig;
import com.atguigu.spring5.jdbctemplate.entity.Order;
import com.atguigu.spring5.jdbctemplate.service.BookService;
import com.atguigu.spring5.service.UserService;
import org.aspectj.weaver.ast.Or;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.GenericApplicationContext;

import java.util.function.Supplier;

public class TestDemo01 {


    @Test
    public void test01() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        UserService userService = context.getBean("userService", UserService.class);

        System.out.println(userService);
        userService.addUser();
    }

    @Test
    public void test02() {
        ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
        UserService userService = context.getBean("userService", UserService.class);

        System.out.println(userService);
        userService.addUser();
    }

    @Test
    public void testAnoAnno() {
        ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
//        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");

        User user = context.getBean("user", User.class);
        user.add();
    }

    @Test
    public void jdbcAdd() {
          ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        BookService bookService = context.getBean("bookService", BookService.class);
        Order order = new Order();
        order.setId(999);
        order.setOrder_no("牛的一批的订单号");
        bookService.addOrder(order);
        System.out.println("执行完成！");


    }
    @Test
    public void jdbcAdd2() {
        GenericApplicationContext context = new GenericApplicationContext();
        context.refresh();
        //通过 spring5 将new 的对象在spring中注册
        context.registerBean("敦佳星",Order.class, () -> new Order());
        // 获取在spring 中注册的对象
        Order bean = context.getBean(Order.class);
        System.out.println("名称为："+bean);
        System.out.println(context.getBean("敦佳星"));

    }
}
